// pages/Accounts/Sign/Sign.js

const controllers = require('../../controllers/controllerIndex.js').controller;
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHiddle: false,
    accountsValue: '收入',
    accounts: [
      {name: '收入', value: 1, checked:'true'},
      {name: '支出', value: 2}
    ],
    pickerConfig: {
      endDate: false,
      column: "minute",
      dateLimit: true,
      initStartTime: "2019-01-01 12:32:44",
      limitStartTime: "2015-05-06 12:32:44"
     
    },
      abbreviation: '',
      nick: '',
      mobile: '',
      money: '',
      billTime: '',
      type: 0,
      desc: ''
  },
  setPickerTime: function(val) {
    console.log(val);
    let data = val.detail;
    let startTime=/\d{4}-\d{1,2}-\d{1,2}/g.exec(data.startTime)
  
    this.setData({
      billTime: data.startTime,
      isPickerShow: false,
    });
  }, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //记录
    let newTime=new Date();
    this.setData({
     
      billTime: newTime.getFullYear()+'-'+  (newTime.getMonth()+1)+'-'+ newTime.getDate()+" "+newTime.getHours()+":"+newTime.getMinutes(),
      
      'pickerConfig.initStartTime':newTime.getFullYear()+'-'+  (newTime.getMonth()+1)+'-'+ newTime.getDate()+" "+newTime.getHours()+":"+newTime.getMinutes()
     })
  },
  pickerShow: function() {
    this.setData({
      isPickerShow: true,
      isPickerRender: true,
      chartHide: true
    });
  },
  selectHiddel() {
    console.log(this.data.accounts)
    this.setData({
      isHiddle: !this.data.isHiddle
    })
  },
  radioChange(e) {
     console.log(e)
    const accounts = this.data.accounts
    let accountsValue = this.data.accountsValue
    
    for(let i = 0; i < accounts.length; i++) {
      accounts[i].checked=false
        if(accounts[i].value == e.detail.value){
          accounts[i].checked=true
        
          accountsValue=accounts[i].name
          
        }
    }
     
  
    this.setData({
      isHiddle: false,
      accounts:accounts,
      accountsValue
    })
  },

  // 提交表单
  formSubmit(e) {
    let info = e.detail.value
    if(info.type==null){
      info.type=0
    }
    let accounts= this.data.accounts
    for(let i = 0; i < accounts.length; i++) {
      
      if(accounts[i].checked ==true){
        info.type=i
      }
    }
    info.sessionId = wx.getStorageSync('token'),
    info.shopId = wx.getStorageSync('shopId'),
    info.mobile=wx.getStorageSync('mobile'),
    info.nick=wx.getStorageSync('adminName'),
    info.billTime=this.data.billTime
    if(!info.abbreviation)
    {
      wx.showToast({
        title: '请先输入记账类目',
      })
      return
    }
    if(!info.money)
    {
      wx.showToast({
        title: '请先输入金额',
      })
      return
    }
    // 记账
    controllers.saveBill(
      info
    ).then(res =>{
     if(res.code==1){
      wx.navigateBack({
  
      })
     }
    })
  },
cancel(){ wx.navigateBack({
  
})},

changeDateTime1(e) {
  // var date=this.data.dateTimeArray1
  var time=e.detail.value
   this.setData({
    billTime: time,
    // startTime:date[0][time[0]]+"-"+date[1][time[1]]+"-"+date[2][time[2]]+" "+date[3][time[3]]+":"+date[4][time[4]]
   });
},
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})